<?php
/**
 * 场外交易管理.
 * User: madong
 * Date: 2018/10/5
 * Time: 5:29 PM
 */

namespace app\api\controller\jiaoyi;

use app\api\model\JiaoyiUinfo;
use app\api\model\Jiaoyibuylog;
use app\api\model\Jiaoyi;
use think\Controller;
use think\Db;
use think\Request;

class Changwaimanages extends Controller
{
    //获取场外上架产品
    public function Get_changwai_sell_order()
    {

        $Jiaoyiobj = new Jiaoyi();

        $res = Request::instance();

        if ($res->isPost()) {

            $order_info = $Jiaoyiobj->where('uname', $res->param('uname'))->where('uid', $res->param('uid'))->where('zt', 1)->select();
            static $data_arr = array();
            foreach ($order_info as $value) {

                $arr = array();
                $arr['id'] = $value['id'];
                $arr['uname'] = $value['uname'];
                $arr['uid'] = $value['uid'];
                $arr['value'] = $value['value'];
                $arr['sysl'] = $value['sysl'];
                $arr['jiage'] = $value['jiage'];
                $arr['xiao'] = $value['xiao'];
                $arr['da'] = $value['da'];
                $arr['truename'] = $value['truename'];
                $arr['yinlian'] = $value['yinlian'];
                $arr['khh'] = $value['khh'];
                $arr['zfb'] = $value['zfb'];
                $arr['weixin'] = $value['weixin'];
                $arr['bizhong'] = $value['bizhong'];
                $arr['type'] = $value['type'];
                $arr['typedescribe'] = $value['typedescribe'];
                $arr['ordernumber'] = $value['ordernumber'];
                $arr['note'] = $value['note'];
                $arr['weixinpic'] = $value['weixinpic'];
                $arr['zfbpic'] = $value['zfbpic'];
                $arr['touxiang'] = $value->UserInfo->headimg;
                $arr['user_num'] = Db::name('jiaoyiUinfo')->where('uname', $value['uname'])->value($value['bizhong']);
                $arr['jiaoyics'] = $value['jiaoyics'];
                $arr['xinren'] = $value['xinren'];
                $arr['haoping'] = $value['haoping'];
                $arr['time'] = $value['time'];
                $arr['wfkdd'] = $value['wfkdd'];
                $arr['yfkdd'] = $value['yfkdd'];
                $arr['jiaoyiok'] = $value['jiaoyiok'];
                $arr['zt'] = $value['zt'];
                $arr['imgurl'] = "http://" . $_SERVER['HTTP_HOST'];
                $data_arr[] = $arr;

            }

            if ($data_arr){

                echo json_encode(['zt' =>1,'msg'=>'获取广告订单获取成功','data'=>$data_arr]);

            }else{

                echo json_encode(['zt'=>0,'msg'=>'您还没有发布过广告!']);

            }

        } else {

            echo json_encode(['zt' => 2, 'msg' => 'error']);

        }

    }

    //获取场外下架产品
    public function Get_changwai_xiajia_order()
    {

        $Jiaoyiobj = new Jiaoyi();

        $res = Request::instance();

        if ($res->isPost()) {

            $order_info = $Jiaoyiobj->where('uname', $res->param('uname'))->where('uid', $res->param('uid'))->where('zt', 0)->select();

            if ($order_info) {

                echo json_encode(['zt' => 1, 'msg' => $order_info]);

            } else {

                echo json_encode(['zt' => 0, 'msg' => '您还没有任何订单!']);

            }

        } else {

            echo json_encode(['zt' => 2, 'msg' => 'error']);

        }

    }

    /*
     * 获取场外已付款和为未付款订单
     *
     */
    public function Get_sell_wfk_order($ordernumber = '', $type = '')
    {

        $jiaoyilogobj = new Jiaoyibuylog();

        if ($ordernumber) {

            if ($type == 0) {//未付款

                $order_data = $jiaoyilogobj->where('uordernumber', $ordernumber)->where('oorderzt', 0)->select();

                echo json_encode(['zt' => 1, 'msg' => $order_data]);

            } elseif ($type == 1) {//已付款

                $order_data = $jiaoyilogobj->where('uordernumber', $ordernumber)->where('oorderzt', 1)->select();

                echo json_encode(['zt' => 1, 'msg' => $order_data]);

            } else {

                echo json_encode(['zt' => 0, 'msg' => 'Type Error']);

            }


        } else {

            echo json_encode(['zt' => 0, 'msg' => '参数错误!']);

        }


    }

    //确认收款

    public function Queren_shoukuan()
    {

        $res = Request::instance();

        if ($res->isPost()) {

            if ($res->param('querenleixing') === 1) {

                Db::startTrans();

                try {

                    Db::name('jiaoyibuylog')->where('ordernumber', $res->param('ordernumber'))->where('uordernumber', $res->param('uordernumber'))->update([

                        'qrsktime' => time(),

                        'oorderzt' => 2,//确认收款状态货币已经转移了

                    ]);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setInc('jiaoyiok', 1);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setInc('jiaoyics', 1);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setDec('yfkdd', 1);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setDec('sysl', $res->param('coinnum'));

                    Db::name('jiaoyi_uinfo')->where('uname', $res->param('oname'))->setDec($res->param('bizhong'), $res->param('coinnum'));

                    Db::name('jiaoyi_uinfo')->where('uname', $res->param('uname'))->setInc($res->param('bizhong'), $res->param('coinnum'));

                    Db::name('jiaoyi_uinfo')->where('uname', $res->param('uname'))->setInc('jiaoyics', 1);

                    Db::commit();

                    echo json_encode(['zt' => 1, 'msg' => '确认完成,本次交易完成']);

                } catch (\Exception $e) {
                    echo json_encode(['zt' => 0, 'msg' => '确认失败!稍后重试']);

                    Db::rollback();
                }

            } elseif ($res->param('querenleixing') == 0) {

                Db::startTrans();

                try {

                    Db::name('jiaoyibuylog')->where('ordernumber', $res->param('ordernumber'))->where('uordernumber', $res->param('uordernumber'))->update([

                        'qrsktime' => time(),

                        'oorderzt' => 2,//确认收款状态货币已经转移了

                    ]);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setInc('jiaoyiok', 1);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setInc('jiaoyics', 1);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setDec('yfkdd', 1);

                    Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setDec('sysl', $res->param('coinnum'));

                    Db::name('jiaoyi_uinfo')->where('uname', $res->param('oname'))->setInc($res->param('bizhong'), $res->param('coinnum'));

                    Db::name('jiaoyi_uinfo')->where('uname', $res->param('uname'))->setDec($res->param('bizhong'), $res->param('coinnum'));

                    Db::name('jiaoyi_uinfo')->where('uname', $res->param('uname'))->setInc('jiaoyics', 1);

                    Db::commit();

                    echo json_encode(['zt' => 1, 'msg' => '确认完成,本次交易完成']);

                } catch (\Exception $e) {

                    echo json_encode(['zt' => 0, 'msg' => '确认失败!稍后重试']);

                    Db::rollback();
                }

            } else {

                echo json_encode(['zt' => 0, 'msg' => '参数错误']);

            }

        } else {

            echo json_encode(['zt' => 2, ',msg' => 'error']);

        }

    }

    //订单取消
    public function Order_quxiao()
    {

        $res = Request::instance();

        if ($res->isPost()) {

            Db::startTrans();

            try {

                Db::name('jiaoyibuylog')->where('ordernumber', $res->param('ordernumber'))->update([

                    'oorderzt' => 4,

                    'quxiaoinfo' => $res->param('quxiaoinfo'),

                ]);

                Db::name('jiaoyi_uinfo')->where('uname', $res->param('quname'))->setInc('quxiaocs', 1);

                Db::name('jiaoyi')->where('ordernumber', $res->param('uordernumber'))->setDec('wfkdd', 1);

                echo json_encode(['zt' => 1, 'msg' => '订单取消成功']);

                Db::commit();

            } catch (\Exception $e) {

                echo json_encode(['zt' => 0, 'msg' => '订单取消失败,请稍后重试']);

                Db::rollback();
            }

        } else {

            echo json_encode(['zt' => 2, 'msg' => 'error']);

        }

    }

    //获取指定订单的详细信息
    public function Get_now_order_info()
    {

        $res = Request::instance();

        $data = Db::name('jiaoyibuylog')->where('ordernumber', $res->param('ordernumber'))->find();

        if ($data) {

            echo json_encode(['zt' => 1, 'msg' => $data]);

        } else {

            echo json_encode(['zt' => 0, 'msg' => '订单获取失败请稍后重试!']);

        }

    }

    //获取收款人的收款信息

    public function Get_shoukuanma()
    {

        $res = Request::instance();

        $dingdan_user = Db::name('jiaoyi')->where('ordernumber', $res->param('ordernumber'))->value('uname');

        $User_sk = Db::name('jiaoyi_uinfo')->where('uname', $dingdan_user)->find();

        if ($dingdan_user) {

            echo json_encode(['zt' => 0, 'msg' => $dingdan_user]);

        } else {

            echo json_encode(['zt' => 0, 'msg' => '订单获取失败']);

        }

    }

    //倒计时订单取消
    public function order_time()
    {

        $res = Request::instance();

        if ($res->isPost()) {

            //订单到期时间，这是用户前端用户设置的订单有效时间 不是时间戳格式是分钟数
            $dqsj = Db::name('jiaoyibuylog')->where('uname', $res->param('uname'))->where('ordernumber', $res->param('ordernumber'))->value('dqsj');

            //下单时间
            $xdsj = Db::name('jiaoyibuylog')->where('uname', $res->param('uname'))->where('ordernumber', $res->param('ordernumber'))->value('time');

            $fen = $dqsj;

            //传入前端的剩余秒数

            $apptime = strtotime('+' . $fen . 'minute', $xdsj) - time();

            if ($apptime > 0) {//如果剩余的时间戳大于0就是没用到时间

                echo json_encode(['zt' => 1, 'msg' => $apptime]);

            } else {//如果返回的时间戳小于0那就是订单到期了，执行订单取消操作

                echo json_encode(['zt' => 0, 'msg' => '']);

            }

        } else {

            echo json_encode(['zt' => 0, 'msg' => 'error']);

        }

    }

    //广告下加
    public function Guanggao_xia()
    {

        $res = Request::instance();

        if ($res->isPost()) {

            if ($res->param('type') == 0) {

                if (Db::name('jiaoyi')->where('ordernumber', $res->param('ordernumber'))->update(['zt' => 0])) {

                    echo json_encode(['zt' => 1, 'msg' => '广告下架成功!']);

                } else {

                    echo json_encode(['zt' => 0, 'msg' => '广告下架失败!']);

                }

            } elseif ($res->param('type') == 1) {

                if (Db::name('jiaoyi')->where('ordernumber', $res->param('ordernumber'))->update(['zt' => 1])) {

                    echo json_encode(['zt' => 1, 'msg' => '广告上架架成功!']);

                } else {

                    echo json_encode(['zt' => 0, 'msg' => '广告上架失败!']);

                }

            } else {

                echo json_encode(['zt' => 0, 'msg' => '错误！']);

            }


        } else {

            echo json_encode(['zt' => 2, 'msg' => 'error']);

        }


    }

}